﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.CRM.Component.Business;
using TRPOP.CRM.Component.Common;
using TRPOP.CRM.Component.Model;
using TRPOP.Common;

namespace TRPOP.CRM.Web
{
    public partial class Company_save : BaseAdmin
    {
        #region 权限限制
        protected override void SubPageInit()
        {
            base.m_oCurrentPermissionKey = "Company_save";
        }
        #endregion

        #region 页面变量
        //记录传递参数CompanyID
        public int m_intCompanyID = 0;
        //添加客户
        public CompanyInfo m_oCompanyInfo = null;
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindInfo();
                if (RequestHelper.RequestInt("id") > 0)
                {
                    m_intCompanyID = RequestHelper.RequestInt("id");
                    BindCompanyInfo();
                }
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            #region 信息验证

            if (txtCompanyName.Text.Trim().Length <= 0)
            {
                Alert("请输入客户姓名");
                txtCompanyName.Focus();
                return;
            }

            #endregion

            //intCompanyID大于0，则更新ID为intCompanyID的Company信息；否则插入Company信息
            if (ViewState["CompanyInfo"] != null)
            {
                m_oCompanyInfo = ViewState["CompanyInfo"] as CompanyInfo;
            }
            else
            {
                m_oCompanyInfo = new CompanyInfo();

                //检测客户名称是否重复
                if (CompanyManage.CheckCompanyNameExist(txtCompanyName.Text.Trim()))
                {
                    Alert("客户姓名不能重复，请输入客户姓名");
                    txtCompanyName.Focus();
                    return;
                }

                m_oCompanyInfo.dtCreationTime = DateTime.Now;
                m_oCompanyInfo.iCreatByEmployee = base.GetCurrentEmployeeInfo().iEmployeeID;
                m_oCompanyInfo.iEmployeeFollowup = base.GetCurrentEmployeeInfo().iEmployeeID;
            }

            //客户基本资料
            m_oCompanyInfo.nvcCompanyName = txtCompanyName.Text.Trim().HtmlEncode();
            m_oCompanyInfo.nvcContactName = txtContactName.Text.Trim().HtmlEncode();

            m_oCompanyInfo.bGender = rbtnlstGender.SelectedValue.ToBool();
            m_oCompanyInfo.nvcDiscription = txtDescription.Text.Trim().HtmlEncode();

            m_oCompanyInfo.iCompanyCategoryID = ddlCompanyCategory.SelectedValue.ToInt();
            m_oCompanyInfo.iCompanyStatus = ddlCompanyStatus.SelectedValue.ToInt();
            m_oCompanyInfo.iCompanyGradeID = ddlCompanyGrade.SelectedValue.ToInt();
            m_oCompanyInfo.iCompanySourceID = ddlCompanySource.SelectedValue.ToInt();
            //m_oCompanyInfo.iEmployeeFollowup = ddlEmployeeFollowup.SelectedValue.ToInt();

            //员工联系方式
            m_oCompanyInfo.nvcRegisterAdress = txtRegisterAdress.Text.Trim().HtmlEncode();
            m_oCompanyInfo.nvcContactAddress = txtContactAddress.Text.Trim().HtmlEncode();
            m_oCompanyInfo.nvcCountry = txtCountry.Text.Trim().HtmlEncode();
            m_oCompanyInfo.nvcProvince = RequestHelper.RequestString("txtProvince");////txtProvince.Value.Trim().HtmlEncode();
            m_oCompanyInfo.nvcCity = RequestHelper.RequestString("txtCity"); //txtCity.Value.Trim().HtmlEncode();
            m_oCompanyInfo.nvcContactPhone = txtContactPhone.Text.Trim().HtmlEncode();
            m_oCompanyInfo.nvcContactTel = txtContactTel.Text.Trim().HtmlEncode();
            m_oCompanyInfo.nvcFax = txtFax.Text.Trim().HtmlEncode();
            m_oCompanyInfo.nvcEmail = txtEmail.Text.Trim().HtmlEncode();
            m_oCompanyInfo.nvcPostCode = txtPostalCode.Text.Trim().HtmlEncode();
            m_oCompanyInfo.nvcQQ = txtQQ.Text.Trim().HtmlEncode();
            m_oCompanyInfo.nvcWebSite = txtWebSite.Text.Trim().HtmlEncode();

            bool bResult = CompanyManage.SaveCompanyInfo(m_oCompanyInfo);

            if (bResult)
            {
                Alert("客户信息保存成功", "Company_AllList.aspx");
            }
            else
            {
                Alert("客户信息保存失败");
            }
        }
        #endregion

        #region 页面方法
        /// <summary>
        /// 绑定基础信息
        /// </summary>
        private void BindInfo()
        {
            //绑定客户状态信息
            PageBindHelper.BindCompanyStatus(ddlCompanyStatus);

            //绑定客户类型信息
            PageBindHelper.BindCompanyCategoryInfo(ddlCompanyCategory);

            //绑定客户等级信息
            PageBindHelper.BindCompanyGradeInfo(ddlCompanyGrade);

            //绑定客户来源信息
            PageBindHelper.BindCompanySourceInfo(ddlCompanySource);

            //绑定员工跟踪类型
            //PageBindHelper.BindFollowUpCategoryInfo(ddlEmployeeFollowup);
        }
        /// <summary>
        /// 绑定客户类型信息
        /// </summary>
        private void BindCompanyInfo()
        {
            m_oCompanyInfo = CompanyManage.GetCompanyInfo(m_intCompanyID);
            if (m_oCompanyInfo != null)
            {
                ViewState["CompanyInfo"] = m_oCompanyInfo;

                //客户的基本信息
                txtCompanyName.Text = m_oCompanyInfo.nvcCompanyName.Trim();
                txtCompanyName.Enabled = false;

                txtContactName.Text = m_oCompanyInfo.nvcContactName.Trim();
                txtContactName.Enabled = false;

                rbtnlstGender.SelectedValue = m_oCompanyInfo.bGender ? "1" : "0";

                txtDescription.Text = m_oCompanyInfo.nvcDiscription.Trim();

                ddlCompanyStatus.SelectedValue = m_oCompanyInfo.iCompanyStatus.ToString();

                ddlCompanyCategory.SelectedValue = m_oCompanyInfo.iCompanyCategoryID.ToString();

                ddlCompanyGrade.SelectedValue = m_oCompanyInfo.iCompanyGradeID.ToString();

                ddlCompanySource.SelectedValue = m_oCompanyInfo.iCompanySourceID.ToString();

                //客户联系方式
                txtRegisterAdress.Text = m_oCompanyInfo.nvcRegisterAdress.Trim();

                txtContactAddress.Text = m_oCompanyInfo.nvcContactAddress.Trim();
                txtCountry.Text = m_oCompanyInfo.nvcCountry.Trim();
                txtProvince.Value = m_oCompanyInfo.nvcProvince.Trim();
                txtCity.Value = m_oCompanyInfo.nvcCity.Trim();
                txtContactPhone.Text = m_oCompanyInfo.nvcContactPhone.Trim();
                txtContactTel.Text = m_oCompanyInfo.nvcContactTel.Trim();
                txtFax.Text = m_oCompanyInfo.nvcFax.Trim();
                txtEmail.Text = m_oCompanyInfo.nvcEmail.Trim();
                txtPostalCode.Text = m_oCompanyInfo.nvcPostCode.Trim();
                txtQQ.Text = m_oCompanyInfo.nvcQQ.Trim();
                txtWebSite.Text = m_oCompanyInfo.nvcWebSite.Trim();
            }
        }
        #endregion
    }
}